home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-09-19 | 9.4 KB | 332 lines | [TEXT/PJMM] |
- {================================================}
- {=============== SATInvaders main unit ================}
- {================================================}
-
- { Example file for Ingemars Sprite Animation Toolkit. }
- { © Ingemar Ragnemalm 1992 }
- { See doc files for legal terms for using this code. }
-
- { SATInvaders is a very simple game demonstrating how to use the Sprite Animation}
- { Toolkit. It is intended as a minimal demonstration, without many features and options}
- { that the other sample program, HeartQuest, has. No high scores or even score, only}
- { one life, doesn't save settings, only one kind of enemy, no special effects like explosions}
- { etc. }
-
- program SATInvaders;
-
- uses
- TransSkel, SAT, GameGlobals, SoundConst, sPlayer, sEnemy, sShot, sMissile;
-
- var
- soundFlag, plotFastFlag: boolean;
-
- { -------------------------------------------------------------------- }
- { Game driver procedures }
- { -------------------------------------------------------------------- }
-
- { Setup a new level. This is called when the game starts and at each new level.}
- procedure SetupLevel (level: integer);
- var
- p: point;
- i, j: integer;
- sp, oldmp: SpritePtr;
- r: rect;
- begin { SetupLevel }
- { Clear the Sprite list }
- sp := sRoot;
- while sp <> nil do
- begin
- oldmp := sp;
- sp := sp^.next;
- KillSprite(oldmp);
- end;
- missileCount := 0; { count variable in mMissile }
- { Create all the enemy sprites for the level, depending on the level number. }
- for i := 0 to (level + 1) do
- for j := 0 to (level div 2) + 1 do
- sp := NewSprite(-3, i * 40 + 2, j * 40 + 0, @HandleEnemy, @SetupEnemy, nil);
-
- { Make the player sprite. }
- sp := NewSprite(2, offSizeH div 2, offSizeV - 40, @HandlePlayer, @SetupPlayer, nil);
- { Copy BackScreen to OffScreen to erase old sprites. }
- CopyBits(BackScreen^.portbits, offscreen^.portbits, offscreen^.portrect, offscreen^.portrect, srcCopy, nil);
- PeekOffScreen;
- {SetPort(GameWind);}
- {CopyBits(offScreen^.portBits, GameWind^.portBits, offScreen^.portRect, offScreen^.portRect, srcCopy, nil);}
- end; { SetupLevel }
-
- { Start a new game. Initialize level, score, number of lives, and call setuplevel to make the first level. }
- procedure StartGame;
- begin
- Level := 1;
- setuplevel(level);
- end;
-
- { Declare forward since we want to call it from MoveIt }
- procedure DoFileMenu (item: integer);
- forward;
-
- { This routine is the game driver. It calls RunSAT repeatedly until the game ends or is paused. }
- { I also read the keyboard here. This could optionally be moved to the "player object" module. }
-
- procedure MoveIt;
- var
- fr, tr, r: Rect;
- pt: Point;
- h: integer;
- truepos: longint;
- n, x: integer; { Are these used? }
- t, l: longint;
- truepos32, bredd32: integer; { Some old bugfix that I no longer remember... }
- truepos19, bredd19: integer;
- theEvent: EventRecord; { för att testa musklick }
- { To check for key clicks with GetKeys: }
- km: KeyMap;
- ignore: OSerr;
- begin
- stillrunning := true; { A flag that tells whether or not to quit this routine. }
-
- HideCursor; { NOTE: No matter how we leave the MoveIt procedure, we should ShowCursor. }
-
- { Main loop! Keep running until the game is paused or ends. }
- while stillrunning = true do
- begin
- t := TickCount;
-
- { Here is the real heart of the loop: call Animator once per loop. It will call all the objects. }
- RunSAT(plotFastFlag);
-
- { All the rest of the main loop is game specific, next level, bonus handling, etc. }
- if globalspeed.h = 0 then
- begin
- downcount := pred(downcount);
- if downcount <= 0 then
- begin
- globalspeed.h := -lasth;
- globalspeed.v := 0;
- turnflag := false;
- end;
- end
- else if turnflag then
- begin
- downcount := 10;
- lasth := globalspeed.h;
- globalspeed.h := 0;
- globalspeed.v := 3;
- end;
-
- if not anymonsters then
- begin
- SATSoundShutUp;
- level := level + 1;
- setuplevel(level);
- end; {if not anymonsters}
-
- { Check for keys being pressed - but don't allow background processing.}
- { If you want background processing, either use GetNextEvent+SystemTask or WaitNextEvent (the modern call).}
- if GetOSEvent(8, theEvent) then { keydown }
- if BitAnd(theEvent.modifiers, cmdKey) <> 0 then
- case char(BitAnd(theEvent.message, charCodeMask)) of
- 'q':
- begin
- {StillRunning := false;}
- SkelWhoa;
- { Do all the things we have to do when we leave MoveIt! }
- SATSoundShutUp; { Dispose of sound channel }
- flushevents(EveryEvent, 0); { To forget events, like mouse clicks etc. }
- ShowCursor;
- exit(MoveIt);
- end;
- 's':
- begin
- DoFileMenu(sound);
- end;
- otherwise
- ;
- end; { case}
-
- { Delay, using TickCount so it doesn't matter how fast our Mac is. }
- while ((TickCount - t) < 3) do
- ;
- end; { while stillrunning (main loop) }
-
- { SetPort(GameWind);}
- { invalrect(Therect);}
-
- while not SATSoundDone do
- SATSoundEvents; {Wait for last sound to complete}
-
- ShowCursor;
- flushevents(EveryEvent, 0); { To forget events, like mouse clicks etc. }
-
- ReportStr('Sorry, game over.');
-
- SATSoundShutUp; { Dispose of sound channel }
- end; { MoveIt }
-
- procedure GameWindUpdate;
- var
- s: str255;
- r: Rect;
- crsr: CursHandle;
- begin
- crsr := GetCursor(WatchCursor);
- SetCursor(crsr^^);
- if SATDepthChangeTest then
- begin
- end;
- ReleaseResource(handle(crsr));
- InitCursor;
-
- PeekOffScreen;
- {SetPort(GameWind);}
- {CopyBits(offScreen^.portBits, GameWind^.portBits, offScreen^.portRect, offScreen^.portRect, srcCopy, nil);}
- end;
-
- { Process selection from File menu.}
-
- procedure DoFileMenu (item: integer);
- begin
- case item of
- run:
- begin
- { Test if we have Color QD, and if so, test bit depth! Alert if features^^.PlotFast.}
- if not ((OurDepth = 1) or (OurDepth = 4) or (OurDepth = 8)) and plotFastFlag then
- begin
- ReportStr('Please uncheck ''Fast animation'' or set the monitor to b/w, 4-bit or 8-bit mode in the Control Panel.');
- exit(DoFileMenu);
- end;
- if SATDepthChangeTest then {Update if necessary}
- ;
- StartGame;
- ShowWindow(gameWind);
- SelectWindow(gameWind);
- GameWindUpdate;
- MoveIt;
- end;
- sound:
- begin
- soundFlag := not soundFlag;
- CheckItem(FileMenu, sound, soundFlag);
- if soundFlag then { Tell the sound package our settings, so we don't have to bother. }
- SATSoundOn
- else
- SATSoundOff;
- end;
- fastAnimation:
- begin
- plotFastFlag := not plotFastFlag;
- CheckItem(fileMenu, fastAnimation, plotFastFlag);
- end;
- quit:
- SkelWhoa;
- end;
- end;
-
- procedure GameWindIdle;
- begin
- if SATDepthChangeTest then
- begin
- PeekOffScreen;
- {SetPort(GameWind);}
- {CopyBits(offScreen^.portBits, gameWind^.portBits, offScreen^.portRect, offScreen^.portRect, srcCopy, nil);}
- end;
- end;
-
- procedure GameWindInit;
- begin
- { Tell TransSkel to tell us when to update GameWind. }
- dummy := SkelWindow(GameWind, nil, nil, @GameWindUpdate, nil, nil, nil, @GameWindIdle, false);
-
- { Set up the two offscreen GrafPorts "offScreen" and "BackScreen". SAT has a standard}
- { way to do this. Let SAT draw the background PICT for us, too. }
-
- { Call the init routines for all the sprite units! }
- initEnemy;
- initPlayer;
- initMissile;
- initShot;
-
- ShowWindow(gameWind);
- SelectWindow(gameWind);
- { Draw the contents of the window (to give the user something to look at during the rest of startup). }
- PeekOffScreen;
- {CopyBits(offscreen^.portbits, GameWind^.portbits, GameWind^.portrect, GameWind^.portrect, srcCopy, nil);}
- end;
-
- { -------------------------------------------------------------------- }
- { Menu handling procedures }
- { -------------------------------------------------------------------- }
-
- { Handle selection of "About…" item from Apple menu}
-
- procedure DoAbout;
- var
- h: StringHandle;
- ignore: integer;
- begin
- ignore := Alert(aboutAlrt, nil);
- end;
-
- { Initialize menus. Tell TransSkel to process the Apple menu}
- { automatically, and associate the proper procedures with the}
- { File menu.}
-
- procedure SetUpMenus;
- begin
- SkelApple('About SAT Invaders…', @DoAbout);
- fileMenu := GetMenu(fileMenuRes);
- dummy := SkelMenu(fileMenu, @DoFileMenu, nil, true);
- { Set the following flags so they match the menu }
- soundFlag := true;
- plotFastFlag := true;
- end;
-
- { Hide gamewindow on suspend, so the user can get access to disk icons etc. }
-
- procedure DoSuspendResume (b: boolean);
- begin
- if b then
- begin
- showwindow(gameWind);
- selectwindow(gameWind);
- end
- else
- hidewindow(gameWind)
- end;
-
- function DoEvt (e: eventRecord): boolean;
- begin
- if e.what = OSevt then
- begin
- if BAND(BROTL(e.message, 8), $FF) = SuspendResumeMessage then
- DoSuspendResume(BAnd(e.message, 1) <> 0);
- DoEvt := true;
- end
- else
- DoEvt := false;
- end; (* end DoEvent *)
-
- { -------------------------------------------------------------------- }
- { Main }
- { -------------------------------------------------------------------- }
-
- begin
- SkelInit(6, nil); { initialize }
-
- { Init all the different parts of the game. }
- SetUpMenus; { install menu handlers }
- GameWind := InitSAT(129, 128, 512, 322);
-
- GameWindInit; { Init the game window }
- Loadsounds; { preload all sound resources }
-
- { Set the randseed to something that is random enough. }
- randSeed := TickCount;
-
- SkelEventHook(@DoEvt); { handle MultiFinder-events }
-
- SkelMain; { loop 'til Quit selected }
- SkelClobber; { clean up }
- SATSoundShutUp; { Terminate sounds }
- end.